

class BookmarkController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ bookmarkInstanceList: Bookmark.list( params ), bookmarkInstanceTotal: Bookmark.count() ]
    }

    def show = {
        def bookmarkInstance = Bookmark.get( params.id )

        if(!bookmarkInstance) {
            flash.message = "Bookmark not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ bookmarkInstance : bookmarkInstance ] }
    }

    def delete = {
        def bookmarkInstance = Bookmark.get( params.id )
        if(bookmarkInstance) {
            try {
                bookmarkInstance.delete(flush:true)
                flash.message = "Bookmark ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Bookmark ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Bookmark not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def bookmarkInstance = Bookmark.get( params.id )

        if(!bookmarkInstance) {
            flash.message = "Bookmark not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ bookmarkInstance : bookmarkInstance ]
        }
    }

    def update = {
        def bookmarkInstance = Bookmark.get( params.id )
        if(bookmarkInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(bookmarkInstance.version > version) {
                    
                    bookmarkInstance.errors.rejectValue("version", "bookmark.optimistic.locking.failure", "Another user has updated this Bookmark while you were editing.")
                    render(view:'edit',model:[bookmarkInstance:bookmarkInstance])
                    return
                }
            }
            bookmarkInstance.properties = params
            if(!bookmarkInstance.hasErrors() && bookmarkInstance.save()) {
                flash.message = "Bookmark ${params.id} updated"
                redirect(action:show,id:bookmarkInstance.id)
            }
            else {
                render(view:'edit',model:[bookmarkInstance:bookmarkInstance])
            }
        }
        else {
            flash.message = "Bookmark not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def bookmarkInstance = new Bookmark()
        bookmarkInstance.properties = params
        return ['bookmarkInstance':bookmarkInstance]
    }

    def save = {
        def bookmarkInstance = new Bookmark(params)
        if(!bookmarkInstance.hasErrors() && bookmarkInstance.save()) {
            flash.message = "Bookmark ${bookmarkInstance.id} created"
            redirect(action:show,id:bookmarkInstance.id)
        }
        else {
            render(view:'create',model:[bookmarkInstance:bookmarkInstance])
        }
    }
}
